package com.ld.config;

import com.mongodb.DB;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoDatabase;
import org.jongo.Jongo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;

@Configuration
public class JongoConfig {
    @Value("${mongo.host}")
    private String host;
    @Value("${mongo.port}")
    private int port;
    @Value("${mongo.database}")
    private String database;
    @Value("${mongo.username}")
    private String userName;
    @Value("${mongo.password}")
    private String pwd;

    @Bean
    public Jongo jongo(){
        MongoCredential credential = MongoCredential.createCredential(userName,database,pwd.toCharArray());
        ServerAddress serverAddress = new ServerAddress(host,port);
        MongoClient mongoClient = new MongoClient(serverAddress, Arrays.asList(credential));
        DB mongo = mongoClient.getDB(database);
        Jongo jongo = new Jongo(mongo);
        return jongo;
    }
}
